package clinic.ms.controller;
import clinic.ms.entity.*;
import clinic.ms.service.ItemService;
import clinic.ms.service.PatientServiceImpl;
import clinic.ms.service.receptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class reception {
    @Autowired
    receptionService receptService = new receptionService();
    @Autowired
    PatientServiceImpl patientService = new PatientServiceImpl();
    @Autowired
    ItemService itemService = new ItemService();
    @Autowired
    Login login;
    Log log;
    @GetMapping("/toTake")
    public ModelAndView toTake()
    {
        List<Log> logs = receptService.LogList();
        List<Look> lists = new ArrayList<>();
        for (Log u : logs) {
            Look look = new Look();
            look.setLogNo(u.getLogNo());
            look.setPatientNo(u.getPatientNo());
            look.setPatientName(patientService.queryPaByNo(u.getPatientNo()).getPatientName());
            look.setStatus(u.getLogStatus());
            if(look.getStatus().equals("已付款"))
            {
                lists.add(look);
            }
            System.out.println(look);
        }
        ModelAndView modelAndView = new ModelAndView("/Taking");
        modelAndView.addObject("logs",lists);
        return modelAndView;
    }
    @GetMapping("/toPay")
    public ModelAndView toPay()
    {
        List<Log> logs = receptService.LogList();
        List<Look> lists = new ArrayList<>();
        for (Log u : logs) {
            Look look = new Look();
            look.setLogNo(u.getLogNo());
            look.setPatientNo(u.getPatientNo());
            look.setPatientName(patientService.queryPaByNo(u.getPatientNo()).getPatientName());
            look.setStatus(u.getLogStatus());
            if(look.getStatus().equals("未付款"))
            {
                lists.add(look);
            }
            System.out.println(look);
        }
        ModelAndView modelAndView = new ModelAndView("/CollectionMain");
        modelAndView.addObject("logs",lists);
        return modelAndView;
    }
    @GetMapping("/receptionMain")
    public ModelAndView toReceptionMain()
    {
        List<Log> logs = receptService.LogList();
        List<Look> lists = new ArrayList<>();
        for (Log u : logs) {
            Look look = new Look();
            look.setLogNo(u.getLogNo());
            look.setPatientNo(u.getPatientNo());
            look.setPatientName(patientService.queryPaByNo(u.getPatientNo()).getPatientName());
            look.setStatus(u.getLogStatus());
            look.setRemarks(u.getLogRemarks());
            System.out.println(u.getLogRemarks());
            if(look.getStatus().equals("未付款"))
            {
                lists.add(look);
            }
            System.out.println(look);
        }
        ModelAndView modelAndView = new ModelAndView("/receptionMain");
        modelAndView.addObject("lists",lists);
        return modelAndView;
    }

    //ok
    @RequestMapping(value={"/recept/one"})
    public ModelAndView receptone(Integer id){
        ModelAndView modelAndView = new ModelAndView("/receptMain");
        List<Log> list=new ArrayList<>();
        if(id==1) {
            list = receptService.LogListwei();
        }
        if(id==2){
            list = receptService.LogListwei();
        }
        if(id==3){
            list = receptService.LogListar();
        }
     //   List<String> pname = new ArrayList();
        if(list.size()==0){
            modelAndView.addObject("result","无未付款患者");
            return modelAndView;

        }
        List<Patient> pname = new ArrayList<>();
        for (Log u : list) {
           // System.out.println(u.getLogNo());
            pname.add(receptService.getpname(u.getPatientNo())) ;
           // System.out.println(receptService.getpname(u.getPatientNo()).getPatientName());
        //pname.add((Patient) receptService.getpname(u.getPatientNo()));
        }
        modelAndView.addObject("log",list);
        modelAndView.addObject("patient",pname);
       return modelAndView;
    }

   //？？
    @RequestMapping(value={"/recept/find"})
    public String find(Model model,String patientNo,String patientName){
        if(!patientNo.equals("")) {
            List<Log> list = receptService.LogListop(patientNo);
            List<Patient> pname = new ArrayList<>();
            if(list.size()==0)
                return "hh";//编号不存在
            for (Log u : list) {
                System.out.println(u.getLogNo());
                pname.add(receptService.getpname(u.getPatientNo()));
                System.out.println(receptService.getpname(u.getPatientNo()).getPatientName());
            }
            if (!patientName.equals("")) {
                if (!patientNo.equals(pname.get(0).getPatientNo())) {
                    return "hh";//不存在
                }
            }
            model.addAttribute("us", list);
            model.addAttribute("pt", pname);
            return "hh";
        }
        if(!patientName.equals("")){
            List<Patient> pname = receptService.getbyname(patientName);
            if(pname.size()==0)
                return "hh";//不存在
            List<Log> list = receptService.LogListop(pname.get(0).getPatientNo());
            for (Log u : list) {
                System.out.println(u.getLogNo());
               // System.out.println(receptService.getpname(u.getPatientNo()).getPatientName());
            }
                model.addAttribute("us", list);
                model.addAttribute("pt", pname);
            return "hh";
        }
        return "hh";//两者都为空
    }


    //ok

    @PostMapping("/receptionAdd")
    public ModelAndView add(String itemNo, Integer logDNum){
        ModelAndView modelAndView=new ModelAndView("/receptionAdd");
        List<Item> items = itemService.queryByNameAndNo("","");
        log = receptService.getLogMax();
        System.out.println(itemNo+logDNum);
        Item item=receptService.getitembyId(itemNo);
        if(item==null){
            System.out.println("kong");
            modelAndView.addObject("result","该药品不存在");
        }
        if(logDNum>item.getItemNums()){
            System.out.println("notenough"+item.getItemName());
            modelAndView.addObject("result","该药品数量不足");
        }else{
            LogD logd=new LogD();
            receptService.updateItem(itemNo,logDNum);
            System.out.println(log.getLogNo());
            logd.setLogNo(log.getLogNo());
            logd.setLogDNum(logDNum);
            logd.setLogDPrice(item.getItemPrice());
            logd.setLogDType(item.getItemType());
            logd.setItemNo(itemNo);
            receptService.insertLogd(logd);
            modelAndView.addObject("result",item.getItemName()+"添加成功");
            modelAndView.addObject("items",items);
        }
         return modelAndView;
    }
    @GetMapping("/receptionAddP")
    public ModelAndView toReceptionAddP()
    {
        List<Patient> patients = patientService.findAll();
        System.out.println(patients);
        ModelAndView modelAndView = new ModelAndView("/receptionAddP");
        modelAndView.addObject("patients",patients);
        return modelAndView;
    }
    @GetMapping("/addP")
    public ModelAndView addP( String patientNo,String patientRemarks){
        ModelAndView modelAndView=new ModelAndView("/receptionAddP");
        List<Patient> patients = patientService.findAll();
        if(patientNo!=null) {
            System.out.println(patientRemarks);
            modelAndView.addObject("patients",patients);
            log = new Log();
            log.setPatientNo(patientNo);
            log.setLogRemarks(patientRemarks);
            System.out.println(log.getLogRemarks());
            log.setUserName(login.getName().getUserName());//需要获取admin的登录状态
            log.setLogStatus("未付款");
            receptService.insertLog(log);
            modelAndView.addObject("result","添加成功");
        }
        return modelAndView;
    }

    @GetMapping("/toAdd")
    public ModelAndView toAdd()
    {
        ModelAndView modelAndView=new ModelAndView("/receptionAdd");
        List<Item> items = itemService.queryByNameAndNo("","");
        modelAndView.addObject("items",items);
        return modelAndView;
    }

    //ok
    @GetMapping("/finish")
   public ModelAndView finish(){
        ModelAndView modelAndView=new ModelAndView("/receptLook");
        Integer logNo=log.getLogNo();
        String patientNo=log.getPatientNo();
        Patient pname=receptService.getpname(patientNo);
        String patientName=pname.getPatientName();
        List<Look> looks = new ArrayList<>();
        List<LogD> list1=receptService.LogdList(logNo);
        for(LogD u: list1)
        {
            Look look = new Look();
            look.setLogDNo(u.getLogDNo());
            look.setItemNo(u.getItemNo());
            look.setLogDNum(u.getLogDNum());
            look.setItemName(receptService.getitembyId(u.getItemNo()).getItemName());
            looks.add(look);
        }

       /*
        List<Item> pname1 = new ArrayList<>();
        for (LogD u : list1) {
            System.out.println(u.getLogDNo());
            pname1.add(receptService.getitembyId(u.getItemNo()));
            System.out.println(receptService.getitembyId(u.getItemNo()));
        }

        */
        modelAndView.addObject("logNo",logNo);
        modelAndView.addObject("patientNo",patientNo);
        modelAndView.addObject("patientName",patientName);
        modelAndView.addObject("looks",looks);
        return modelAndView;
    }

    @GetMapping("/toLook")
    public ModelAndView toLook(@RequestParam("logNo") Integer logNo,@RequestParam("patientNo") String patientNo){
        ModelAndView modelAndView=new ModelAndView("/receptLook");
        Patient pname=receptService.getpname(patientNo);
        String patientName=pname.getPatientName();
        List<Look> looks = new ArrayList<>();
        List<LogD> list1=receptService.LogdList(logNo);
        for(LogD u: list1)
        {
            Look look = new Look();
            look.setLogDNo(u.getLogDNo());
            look.setItemNo(u.getItemNo());
            look.setLogDNum(u.getLogDNum());
            look.setItemName(receptService.getitembyId(u.getItemNo()).getItemName());
            looks.add(look);
        }
        modelAndView.addObject("logNo",logNo);
        modelAndView.addObject("patientNo",patientNo);
        modelAndView.addObject("patientName",patientName);
        modelAndView.addObject("looks",looks);
        return modelAndView;
    }



    //ok 1/3
    @GetMapping("/edit")
    public ModelAndView edit(Integer logNo,String patientNo,String patientName){
        ModelAndView modelAndView=new ModelAndView("/receptLook");
        List<LogD> list1=receptService.LogdList(logNo);
        List<Look> looks = new ArrayList<>();
        for(LogD u: list1)
        {
            Look look = new Look();
            look.setLogDNo(u.getLogDNo());
            look.setItemNo(u.getItemNo());
            look.setLogDNum(u.getLogDNum());
            look.setItemName(receptService.getitembyId(u.getItemNo()).getItemName());
            looks.add(look);
        }
        modelAndView.addObject("logNo",logNo);
        modelAndView.addObject("patientNo",patientNo);
        modelAndView.addObject("patientName",patientName);
        modelAndView.addObject("looks",looks);

        return modelAndView;

    }
   // /recept/one/editaction?logDNo=6&itemNo=20200630093056&olditemNo=20200630093056&logDNum=20&oldlogDNum=100
    //ok
    @GetMapping("/editLogD")
    public ModelAndView editaction(@RequestParam("logDNo") Integer logDNo,
                                   @RequestParam("itemNo") String itemNo,
                                   @RequestParam("olditemNo") String olditemNo,
                                   @RequestParam("logDNum") Integer logDNum,
                                   @RequestParam("oldlogDNum") Integer oldlogDNum,
                                   @RequestParam("logNo") Integer logNo,
                                   @RequestParam("patientNo") String patientNo,
                                   @RequestParam("patientName") String patientName
                                   ){
        System.out.println(logDNo+itemNo+olditemNo+logDNum+oldlogDNum+logNo+patientName+patientNo);
        ModelAndView modelAndView=new ModelAndView("receptEdit");
        Item item=receptService.getitembyId(itemNo);
        receptService.updateItemadd(olditemNo,oldlogDNum);//先加
        if(item==null){
            modelAndView.addObject("result","不存在该药品");
            return modelAndView;
           // System.out.println("kong");
            //return "hh";//不存在该药品

        }
        if(logDNum>item.getItemNums()){
            System.out.println("notenough"+item.getItemName());
            modelAndView.addObject("result","药品数量不足");
            return modelAndView;
          //  return "hh";//药品数量不足
        }else{
            receptService.updateItem(itemNo,logDNum); //后减
            receptService.updateLogd(itemNo,logDNum,logDNo);//更新logd
        }

        Map<String,Object> reqParam = new HashMap<String,Object>(16);
        reqParam.put("logNo",logNo);
        reqParam.put("patientNo",patientNo);
        reqParam.put("patientName",patientName);
       // reqParam.put("id",id);
        return new ModelAndView(new RedirectView("/edit"),reqParam);
    }

    //ok
    @GetMapping("/editAction")
    public ModelAndView editsamll(@RequestParam("logDNo") Integer logDNo,
                                  @RequestParam("itemNo") String itemNo,
                                  @RequestParam("logDNum") Integer logDNum,
                                  @RequestParam("logNo") Integer logNo,
                                  @RequestParam("patientNo") String patientNo,
                                  @RequestParam("patientName") String patientName){
        ModelAndView modelAndView=new ModelAndView("/receptEdit");
        List<Item> items = itemService.queryByNameAndNo("","");
        modelAndView.addObject("items",items);
        modelAndView.addObject("logDNo",logDNo);
        modelAndView.addObject("olditemNo",itemNo);
        modelAndView.addObject("oldlogDNum",logDNum);
        modelAndView.addObject("logNo",logNo);
        modelAndView.addObject("patientNo",patientNo);
        modelAndView.addObject("patientName",patientName);
        return modelAndView;
    }



    //   /recept/one/deleteaction?logDNo=1
    //ok
    @RequestMapping("/deleteAction")
    @ResponseBody
    public void deleteaction(Integer logDNo,
                                     Integer logNo,
                                     String patientNo,
                                     String patientName){
        receptService.deleteLogD(logDNo);
        /*
        Map<String,Object> reqParam = new HashMap<String,Object>(16);
        reqParam.put("logNo",logNo);
        reqParam.put("patientNo",patientNo);
        reqParam.put("patientName",patientName);

         */
       // reqParam.put("id",id);
        edit(logNo,patientNo,patientName);
    }

    //ok
    @GetMapping("/delLog")
    public ModelAndView delete(Integer logNo){
        List<LogD> list=receptService.LogdList(logNo);
        for (LogD u : list) {
            receptService.updateItemadd(u.getItemNo(),u.getLogDNum());//先加
            receptService.deleteLogD(u.getLogDNo());
        }
        receptService.deleteLog(logNo);
        return new ModelAndView(new RedirectView("/receptionMain"));
    }

    @GetMapping("/toPayLook")
    public ModelAndView paylook(@RequestParam("logNo") Integer logNo,@RequestParam("patientNo") String patientNo){
        ModelAndView modelAndView=new ModelAndView("/PayLook");
        Patient pname=receptService.getpname(patientNo);
        String patientName=pname.getPatientName();
        float x = 0;
        List<Look> looks = new ArrayList<>();
        List<LogD> list1=receptService.LogdList(logNo);
        for(LogD u: list1)
        {
            Look look = new Look();
            look.setItemNo(u.getItemNo());
            look.setLogDNum(u.getLogDNum());
            look.setLogDPrice(u.getLogDPrice());
            look.setItemType(u.getLogDType());
            look.setItemSpec(itemService.queryByNo(u.getItemNo()).getItemSpec());
            look.setItemUnit(itemService.queryByNo(u.getItemNo()).getItemUnit());
            look.setItemName(receptService.getitembyId(u.getItemNo()).getItemName());
            look.setItemMadein(itemService.queryByNo(u.getItemNo()).getItemMadein());
            looks.add(look);
        }
        modelAndView.addObject("logNo",logNo);
        modelAndView.addObject("patientNo",patientNo);
        modelAndView.addObject("patientName",patientName);
        modelAndView.addObject("looks",looks);
           for(LogD u:list1){
               x+=u.getLogDNum()*u.getLogDPrice();
           }
           modelAndView.addObject("total",x);
            return modelAndView;

        }

    @GetMapping("/pay")
    public ModelAndView pay(Integer logNo){
        System.out.println(logNo);
        String logStatus="已付款";
        receptService.updateLog(logNo,logStatus);
        return new ModelAndView(new RedirectView("/toPay"));
    }

    @GetMapping("/toTakeLook")
    public ModelAndView fetchlook(@RequestParam("logNo") Integer logNo,@RequestParam("patientNo") String patientNo){
        ModelAndView modelAndView=new ModelAndView("/TakeLook");
        Patient pname=receptService.getpname(patientNo);
        String patientName=pname.getPatientName();
        List<Look> looks = new ArrayList<>();
        List<LogD> list1=receptService.LogdList(logNo);
        for(LogD u: list1)
        {
            Look look = new Look();
            look.setItemNo(u.getItemNo());
            look.setLogDNum(u.getLogDNum());
            look.setLogDPrice(u.getLogDPrice());
            look.setItemType(u.getLogDType());
            look.setItemSpec(itemService.queryByNo(u.getItemNo()).getItemSpec());
            look.setItemUnit(itemService.queryByNo(u.getItemNo()).getItemUnit());
            look.setItemName(receptService.getitembyId(u.getItemNo()).getItemName());
            look.setItemMadein(itemService.queryByNo(u.getItemNo()).getItemMadein());
            looks.add(look);
        }
        modelAndView.addObject("logNo",logNo);
        modelAndView.addObject("patientNo",patientNo);
        modelAndView.addObject("patientName",patientName);
        modelAndView.addObject("looks",looks);
        return modelAndView;
    }

    @GetMapping("/Take")
    public ModelAndView fetch(Integer logNo){
        String logStatus="已取药";
        receptService.updateLog(logNo,logStatus);
      //  receptService.updateLogdf(logNo);
        return new ModelAndView(new RedirectView("/toTake"));

    }

    @GetMapping("/logChart")
    public ModelAndView getLogChart(){
        ModelAndView modelAndView = new ModelAndView("/logChart");
        List<Pie> pieList = receptService.queryReceived("2019-08","2020-08");
        modelAndView.addObject("pieList",pieList);
        return modelAndView;
    }
    @PostMapping("/logChart")
    public ModelAndView logChart(@RequestParam("begin")String begin,
                                 @RequestParam("end")String end){
        ModelAndView modelAndView = new ModelAndView("/logChart");
        List<Pie> pieList = receptService.queryReceived(begin,end);
        modelAndView.addObject("pieList",pieList);
        return modelAndView;
    }
}
